package com.share.sharelibrary.share;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Toast;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.share.sharelibrary.MultiTypeAdapter;
import com.share.sharelibrary.R;
import com.share.sharelibrary.databinding.ActivityShareLayoutBinding;
import com.share.sharelibrary.util.ShareSDKHelp;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;

import java.util.Arrays;
import java.util.List;


/**
 * @author QiQi
 * @description 分享面板
 */

@Route(path = "/share/share")
public class ShareBroadView extends AppCompatActivity implements View.OnClickListener, ShareItem.OnShareItemClickListener {

    public static final String ACTION_SHARE_DATA = "shareData";

    @Autowired
    ShareData shareData;

    ShareSDKHelp shareUtils;
    MultiTypeAdapter adapter;


    ActivityShareLayoutBinding mBinding;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
        super.onCreate(savedInstanceState);
        ARouter.getInstance().inject(this);
        mBinding = DataBindingUtil.setContentView(this, getLayout());
        initData(savedInstanceState);
        initEvent();
    }



    /**
     * 子Activity的布局文件Id
     *
     * @return
     */
    protected int getLayout() {
        return R.layout.activity_share_layout;
    }

    protected void initData(Bundle savedInstanceState) {
        shareUtils = new ShareSDKHelp(this);
        adapter = new MultiTypeAdapter();
        mBinding.shareItemList.setAdapter(adapter);

        final GridLayoutManager manager = new GridLayoutManager(this, 4);
        mBinding.shareItemList.setLayoutManager(manager);

    }


    protected void initEvent() {

        mBinding.cancelBtn.setOnClickListener(this);
        mBinding.nullView.setOnClickListener(this);

        //设置要分享的平台
        setPlatForm(SHARE_MEDIA.WEIXIN, SHARE_MEDIA.WEIXIN_CIRCLE, SHARE_MEDIA.SINA, SHARE_MEDIA.QQ);
    }

    private void setPlatForm(SHARE_MEDIA... items) {
        List<SHARE_MEDIA> shareMediaList = Arrays.asList(items);
        for (SHARE_MEDIA media : shareMediaList) {
            ShareItem item = new ShareItem(this, media.toSnsPlatform());
            item.setOnShareItemClickListener(this);
            adapter.addItem(item);
        }
        adapter.notifyItemRangeInserted(0, items.length);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            exit();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    private void exit() {
        Animation exitAnim = AnimationUtils.loadAnimation(this, R.anim.anim_exit_bottom);
        exitAnim.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                ShareBroadView.this.finish();
                overridePendingTransition(R.anim.hold, R.anim.hold);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
        mBinding.shareBoardLayout.startAnimation(exitAnim);
    }

    @Override
    public void finish() {
        super.finish();
        shareUtils.onDestroy();
    }

    /**
     * Called when a view has been clicked.
     *
     * @param view The view that was clicked.
     */
    @Override
    public void onClick(View view) {
        int i = view.getId();
        if (i == R.id.cancelBtn || i == R.id.nullView) {
            exit();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
    }

    /**
     * 调用分享
     *
     * @param platform 要分享的平台
     */
    @Override
    public void onClick(SHARE_MEDIA platform) {
        if (shareData == null) {
            throw new NullPointerException("share data must not be null");
        }

//        shareUtils.directShare(platform, shareData, new DefaultListener());
    }

    private class DefaultListener implements UMShareListener {

        @Override
        public void onStart(SHARE_MEDIA shareMedia) {

        }

        @Override
        public void onResult(SHARE_MEDIA shareMedia) {
            Toast.makeText(ShareBroadView.this.getApplicationContext(), "分享成功", Toast.LENGTH_SHORT).show();
            finish();
        }

        @Override
        public void onError(SHARE_MEDIA shareMedia, Throwable throwable) {
            Toast.makeText(ShareBroadView.this.getApplicationContext(), "分享失败", Toast.LENGTH_SHORT).show();

        }

        @Override
        public void onCancel(SHARE_MEDIA shareMedia) {
            Toast.makeText(ShareBroadView.this.getApplicationContext(), "分享取消", Toast.LENGTH_SHORT).show();
            finish();
        }
    }

}
